package com.xb.loan.cashier.biz.pay.wechat.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 商户server调用统一下单接口请求订单，
 *          微信会返回给商户prepayid，商户按固定格式生成deeplink，
 *          通过用户点击deeplink来调起微信支付
 *
 */
public class WeChatDeepLink {
    
    // 字符编码格式 目前支持 gbk 或 utf-8
    public static final String CHARSET = "UTF-8";
    
    /**
     * 创建用户点击的deeplink发起的微信支付链接
     *
     * @throws UnsupportedEncodingException
     */
    public static String createDeepLink(Map<String , Object> map) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer();
        
        Iterator<Entry<String, Object>> iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<String, Object> entry = iter.next();
            String key = entry.getKey();
            Object val = entry.getValue();
            
            if (val != null && !"".equals(val)) {
                if (!"".equals(sb.toString())) {
                    sb.append("&");
                }
                sb.append(key).append("=").append(URLEncoder.encode(val.toString(), CHARSET));
            }
        }
        
        return "weixin://wap/pay?" + URLEncoder.encode(sb.toString() , CHARSET);
    }
}
